package main

import "net/http"
import "io"
import "os"
import "bufio"
import "fmt"
import "strconv"
import "time"
import "sync"
import "strings"
import "github.com/PuerkitoBio/goquery"

func savePic(url string,savePath string){
    // function : 保存图片
    imgPath := savePath
    imgUrl := url
    
    res, err := http.Get(imgUrl)
    if err != nil {
        fmt.Println("A error occurred!")
        return
    }
    defer res.Body.Close()
    // 获得get请求响应的reader对象
    reader := bufio.NewReaderSize(res.Body, 32 * 1024)
    
    
    file, err := os.Create(imgPath)
    if err != nil {
        panic(err)
    }
    // 获得文件的writer对象
    writer := bufio.NewWriter(file)
    
    written, _ := io.Copy(writer, reader)
    fmt.Printf("Total length: %d", written)
}

func getPicLink(pageLink string,pageNum int,saveCate string,wg *sync.WaitGroup){
	// 获取图片链接
	defer wg.Done()
	var tampLink string
	var picLink string
	for i:=0;i<pageNum;i++{
		tampLink=pageLink+strconv.Itoa(i)+".html#img"
		resp,err:=http.Get(tampLink)
		if err != nil{
			fmt.Println("获取页面报错 :",err)
		}

		//  html分析
		doc,err:=goquery.NewDocumentFromReader(resp.Body)
		if err!=nil{
			fmt.Println("HTML错误",err)
		}

		// 拿图片连接
		doc.Find("img").Each(func(i int,s *goquery.Selection){
			picLink_,isExist:=s.Attr("src")
			if isExist==true{
				if strings.Index(picLink_,"https://img.cct58.com/statics/images/hd/jzz.gif")==-1{
					// 获取到正确的网址
					picLink=picLink_
				}
			}
			
		})
		savePic(picLink,saveCate+strconv.Itoa(i)+".jpg")
	}
}

func main(){
	var go_sync sync.WaitGroup
	var catelogue string = "F:/资料/cct58/"
	
	beforeList := map[string]int  {
		"https://www.cct58.com/xiezhen/13236/mx27id13236pg":51,
		"https://www.cct58.com/xiezhen/13959/mx27id13959pg":17,
		"https://www.cct58.com/xiezhen/7731/mx27id7731pg":42,
		"https://www.cct58.com/mneinv/19556/mx27id4813pg":53,
		"https://www.cct58.com/mneinv/19556/mx27id18230pg":35,
		"https://www.cct58.com/mneinv/19556/mx27id15631pg":10,
		"https://www.cct58.com/mneinv/19556/mx27id15632pg":13,
		"https://www.cct58.com/mneinv/19556/mx27id16652pg":34,
		"https://www.cct58.com/mneinv/19556/mx27id15570pg":39,
		"https://www.cct58.com/mneinv/19556/mx27id17033pg":34,
		"https://www.cct58.com/mneinv/19556/mx27id17146pg":41,
		"https://www.cct58.com/mneinv/19556/mx27id15629pg":35,
		"https://www.cct58.com/mneinv/19556/mx27id17249pg":48,
		"https://www.cct58.com/mneinv/19556/mx27id7993pg":69,
		"https://www.cct58.com/mneinv/19650/mx27id763pg":5,
		"https://www.cct58.com/mneinv/36592/mx27id17269pg":50,
		"https://www.cct58.com/mneinv/19522/mx27id4063pg":51,
		"https://www.cct58.com/mneinv/19522/mx27id8064pg":56,
		"https://www.cct58.com/mneinv/19468/mx27id4952pg":63,
		"https://www.cct58.com/mneinv/19558/mx27id18364pg":21,
		"https://www.cct58.com/mneinv/19558/mx27id18417pg":21,
		"https://www.cct58.com/mneinv/19558/mx27id16918pg":39,
		"https://www.cct58.com/mneinv/19558/mx27id7920pg":63,
		"https://www.cct58.com/mneinv/19558/mx27id8068pg":52,
		"https://www.cct58.com/xiezhen/17092/mx27id17092pg":36,
		"https://www.cct58.com/xiezhen/17092/mx27id8483pg":44,
		"https://www.cct58.com/xiezhen/14082/mx27id14082pg":41,
	}
	var i int
	i=0
	for k,v:=range beforeList{
		go_sync.Add(1)
		fmt.Println(k)
		err:=os.Mkdir(catelogue+strconv.Itoa(i),os.ModePerm)
		if err!=nil{
			fmt.Println("创建文件夹报错")
		}
		tampPath:=catelogue+strconv.Itoa(i)+"/"
		go getPicLink(k,v,tampPath,&go_sync)
		time.Sleep(time.Second * 30)
		i++
	}
	go_sync.Wait()
	fmt.Println("cct58 爬虫结束")
}